package com.silverbox.sweeporder.service.employee.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.silverbox.core.result.CoreResponse;
import com.silverbox.core.util.CalendarUtil;
import com.silverbox.shopper.entity.Employee;
import com.silverbox.shopper.entity.EmployeeAuthRecord;
import com.silverbox.shopper.mapper.EmployeeAuthRecordMapper;
import com.silverbox.shopper.mapper.EmployeeMapper;
import com.silverbox.sweeporder.controller.member.vo.EmployeeAuthRecordEntryVO;
import com.silverbox.sweeporder.controller.member.vo.FindEmployeeEntryVO;
import com.silverbox.sweeporder.service.employee.EmployeeService;

@Service
//@Transactional
public class EmployeeServiceImpl implements EmployeeService {

	@Autowired
	private EmployeeMapper employeeMapper;
	
	@Autowired
	private EmployeeAuthRecordMapper employeeAuthRecordMapper;

	@Override
	public CoreResponse<FindEmployeeEntryVO> getShopEmployeeIdByMobile(Integer shopId, Integer storeId, String mobile) {
		CoreResponse<FindEmployeeEntryVO>  res = new CoreResponse<FindEmployeeEntryVO>();
		FindEmployeeEntryVO findEmployeeEntryVO = new FindEmployeeEntryVO();
		List<Employee> list = employeeMapper.findEmployeeByMobile(shopId, storeId, mobile);
		if (null !=list && list.size()>0) {
			findEmployeeEntryVO.setSuccess(true);
			findEmployeeEntryVO.setIsExist(true);
			findEmployeeEntryVO.setEmployeeId(Integer.valueOf(list.get(0).getId()));
			
		} else {
			findEmployeeEntryVO.setSuccess(true);
			findEmployeeEntryVO.setIsExist(false);
		}
		res.setData(findEmployeeEntryVO);
		return res;
	}

	@Override
	public CoreResponse<EmployeeAuthRecordEntryVO> isExistEmployeeAuthRecord(Integer shopId, Integer storeId, String source, String appId,
			String buyerId, Integer type) {
		CoreResponse<EmployeeAuthRecordEntryVO>  res = new CoreResponse<EmployeeAuthRecordEntryVO>();
		EmployeeAuthRecordEntryVO employeeAuthRecordEntryVO = new EmployeeAuthRecordEntryVO();
		
		Date authActiveTime=null;
		if(type==1){
			authActiveTime =new Date();
		}
		List<EmployeeAuthRecord> list = employeeAuthRecordMapper.isExistEmployeeAuthRecord(shopId,storeId,source,appId,buyerId,type,authActiveTime);
		if (null !=list && list.size()>0) {
			employeeAuthRecordEntryVO.setSuccess(true);
			employeeAuthRecordEntryVO.setIsExist(true);
			employeeAuthRecordEntryVO.setEmployeeAuthRecordId(Integer.valueOf(list.get(0).getId()));
		} else {
			employeeAuthRecordEntryVO.setSuccess(true);
			employeeAuthRecordEntryVO.setIsExist(false);
		}
		res.setData(employeeAuthRecordEntryVO);
		return res;
	}

	@Override
	public Boolean saveOrUpdateEmployeeAuthRecord(Integer employeeAuthId, Integer shopId, Integer storeId,
			Integer employeeId, String source, String appId, String buyerId) {
		Date date = new Date();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);//设置起时间
		cal.add(Calendar.YEAR, 1);//增加一年
		//System.out.println("输出::"+cal.getTime());
		Date authActiveTime = cal.getTime();
		Date authModifyTime = new Date();
		EmployeeAuthRecord employeeAuthRecord = new EmployeeAuthRecord();
		employeeAuthRecord.setAuthAppid(appId);
		employeeAuthRecord.setAuthUserCode(buyerId);
		employeeAuthRecord.setShopId(shopId);
		employeeAuthRecord.setStoreId(storeId);
		employeeAuthRecord.setAuthSource(source);
		employeeAuthRecord.setAuthModifyTime(authModifyTime);
		employeeAuthRecord.setAuthActiveTime(authActiveTime);
		employeeAuthRecord.setEmployeeId(employeeId);
		if(null != employeeAuthId){
			employeeAuthRecord.setId(employeeAuthId);
			//更新box_employee_auth_record表
			//employeeAuthRecordMapper.updateByPrimaryKey(employeeAuthRecord);
			employeeAuthRecordMapper.updateEmployeeAuthRecordById(employeeAuthId,authActiveTime,authModifyTime);
		}else{
			//插入box_employee_auth_record表
			employeeAuthRecord.setAuthCreateTime(new Date());
			employeeAuthRecordMapper.insert(employeeAuthRecord);
		}
		return true;
		
	}

	@Override
	public Employee getEmployeeEntry(Integer shopId, Integer storeId,String source, String appId, String buyerId) {
		String date = CalendarUtil.DateToString(new Date());
		return employeeMapper.getEmployeeEntry(shopId, storeId, source, appId, buyerId,date);
	}
}
